package com.yh.correct.controller;

import java.util.*;

import com.github.pagehelper.PageInfo;
import com.yh.correct.model.*;
import com.yh.correct.service.ICorrectedPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yh.correct.service.IMessageService;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 信息管理控制层
 *
 * @author 崔赛赛
 */
@RestController
@RequestMapping("/information")

public class MessageController {
    /**
     * 实例化信息服务接口
     */
    @Autowired
    private IMessageService iMessageService;
    /**
     * 注入service
     */
    @Autowired
    private ICorrectedPersonService correctedPersonService;


    /**
     * 跳转到短信管理页面
     *
     * @return
     */
    @RequestMapping(value = "/show")
    public ModelAndView toShow() {
        ModelAndView mv = new ModelAndView("/message/manager");
        return mv;
    }




    /**
     * 跳转到更新角色页面
     *
     * @return
     */
    @RequestMapping(value = "/fa")
    public ModelAndView toEditRole() {
        ModelAndView mv = new ModelAndView("/message/manager_fa");
        return mv;
    }


    /*
    * 获得人员列表
    * */
    @RequestMapping("psersion")
    public ResultData psersion(String phonenum, Integer pageNum, Integer pageSize) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("pageNum", pageNum);
        condition.put("pageSize", pageSize);
        condition.put("phonenum", phonenum);
        PageInfo<Object> pageInfo = null;
        try {
            pageInfo = correctedPersonService.getPersion(condition);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<Object> list = new ArrayList<Object>();
        list.add(pageInfo);
        ResultData rd = new ResultData();
        rd.setCode(0);
        rd.setMessage("查询成功！");
        rd.setData(list);
        return rd;
    }


    /**
     * 添加信息
     */
    @RequestMapping("/add")
    public ResultData add(Message message) {

        Message me = new Message();
        me.setContent("你很好1156555556");
        me.setSendTime(new Date());
        me.setStatus(0);
        me.setSendId(2);
        //me.setTelephone("18613717382");
        me.setReceiveId(155);

        ResultData rd = new ResultData();
        List<Message> m = new ArrayList<Message>();
        m.add(me);
        int i = iMessageService.addMessage(me);
        if (i > 0) {
            rd.setCode(0);
            rd.setMessage("插入成功！");
            rd.setData(m);
        } else {

            rd.setCode(-1);
            rd.setMessage("插入0！");

        }
        return rd;
    }

    /**
     * 删除信息
     */
    @RequestMapping("/del")
    public ResultData del(Message message) {
        Integer id = 4;
        iMessageService.delMessage(id);
        ResultData rd = new ResultData();
        rd.setCode(0);
        rd.setMessage("删除成功！");

        return rd;


    }

    /**
     * 编辑信息
     */
    @RequestMapping("/edit")
    public ResultData edit(Message message) {
        Message me = new Message();
        me.setContent("李凯你老爸让我抓你回家 ");
        me.setSendTime(new Date());
        me.setStatus(0);
        me.setSendId(2);
        //me.setTelephone("18613717382");
        me.setReceiveId(186134856);
        me.setId(5);
        List<Message> ms = new ArrayList<Message>();
        ms.add(me);
        int i = iMessageService.editMessage(me);
        ResultData rd = new ResultData();
        if (i > 0) {
            rd.setCode(0);
            rd.setMessage("修改成功！");
            rd.setData(ms);
        } else {

            rd.setCode(-1);
            rd.setMessage("修改失败！");
            rd.setData(ms);
        }
        return rd;

    }

    /**
     * 查询所有信息
     */
    @RequestMapping("/getAll")
    public ResultData getAll(Message message) {
        List<Message> m = iMessageService.getALLMessage();
        ResultData rd = new ResultData();
        rd.setCode(0);
        rd.setMessage("查看成功！");

        rd.setData(m);
        return rd;

    }

    /**
     * 根据id查询信息
     */
    @RequestMapping("/get")
    public ResultData get(Message message) {
        ResultData rd = new ResultData();
        List<Message> mm = new ArrayList<Message>();
        Message i = iMessageService.getMessage(5);
        mm.add(i);
        rd.setCode(0);
        rd.setMessage("查询成功！");
        rd.setData(mm);
        return rd;
    }


    /*
    * 动态查找角色分页*/
    @RequestMapping("searchAll")
    public ResultData searchRole(String roleName, Integer pageNum, Integer pageSize) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("pageNum", pageNum);
        condition.put("pageSize", pageSize);
        condition.put("roleName", roleName);
        PageInfo<Object> pageInfo = null;
        try {
            pageInfo = iMessageService.findListByMap(condition);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<Object> list = new ArrayList<Object>();
        list.add(pageInfo);
        ResultData rd = new ResultData();
        rd.setCode(0);
        rd.setMessage("查询成功！");
        rd.setData(list);
        return rd;
    }







}
